package src.linkeLlist;

public class no203 {
    //移除链表元素，带虚拟头结点
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = null;
        ListNode head = removeElements(node1, 3);
        System.out.println(head.next.next.val);

    }

    public static ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode p = dummy;
        while (p.next != null) {
            if(p.next.val == val){
                p.next=p.next.next;
            }else {
                p=p.next;
            }
        }
        return dummy.next;

    }
}
